package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Gallant
 * @create 2020-07-12 08:17
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    @Override
    public Integer findMemberCountByMonth(String month) {
        month = month +".31";
        return memberDao.findMemberCountBeforeDate(month);
    }

    @Override
    //会员性别
    public List<Map<String, Object>> findMemberGenderCount() {
        return memberDao.findMemberGenderCount();
    }

    @Override
    //会员年龄
    public Map<String, Object> findMemberAgeCount() {
        // map key 是 0-18   18 -30  30-45 45以上
        //前端需要setmealNames    setmealCount (name , value)
        Map<String, Object> memberAgeCount = memberDao.findMemberAgeCount();
        BigDecimal age1 = (BigDecimal) memberAgeCount.get("0-18");
        BigDecimal age2 = (BigDecimal) memberAgeCount.get("18-30");
        BigDecimal age3 = (BigDecimal) memberAgeCount.get("30-45");
        BigDecimal age4 = (BigDecimal) memberAgeCount.get("45以上");
        List setmealNames =new ArrayList();
        setmealNames.add("0-18");
        setmealNames.add("18-30");
        setmealNames.add("30-45");
        setmealNames.add("45以上");
        List<Map>  setmealCount =new ArrayList<>();
        Map mapAge1 =new HashMap();
        mapAge1.put("name","0-18");
        mapAge1.put("value",Integer.parseInt(String.valueOf(age1)));

        Map mapAge2 =new HashMap();
        mapAge2.put("name","18-30");
        mapAge2.put("value",Integer.parseInt(String.valueOf(age2)));

        Map mapAge3 =new HashMap();
        mapAge3.put("name","30-45");
        mapAge3.put("value",Integer.parseInt(String.valueOf(age3)));

        Map mapAge4 =new HashMap();
        mapAge4.put("name","45以上");
        mapAge4.put("value",Integer.parseInt(String.valueOf(age4)));
        setmealCount.add(mapAge1);
        setmealCount.add(mapAge2);
        setmealCount.add(mapAge3);
        setmealCount.add(mapAge4);
        Map result  =new HashMap();
        result.put("setmealNames",setmealNames);
        result.put("setmealCount",setmealCount);
        return result;
    }

    @Override
    public Map getMemberReportByDate(Map map) throws Exception {
        String start = (String) map.get("startTime");//2020-02-29T16:00:00.000Z
        String end = (String) map.get("endTime");//2020-06-30T16:00:00.000Z
        String startTime = start.substring(0,start.lastIndexOf("-"))+"-1";
        String endTime = end.substring(0,end.lastIndexOf("-"))+"-31";
        List<String> monthBetween = DateUtils.getMonthBetween(startTime, endTime, "yyyy-MM");
        List<String> months =new ArrayList<>();
        List<Integer> memberCount = new ArrayList<>();
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM");
        for (String s : monthBetween) {
            Date parse = simpleDateFormat2.parse(s);
            parse.setMonth(parse.getMonth()+1);
            String format = simpleDateFormat2.format(parse);
            String date = format+"-31";
            Integer count =   memberDao.findMemberCountBeforeDate(date);
            months.add(format);
            memberCount.add(count);
        }
        Map result =new HashMap();
        result.put("months",months);
        result.put("memberCount",memberCount);
        return result;
    }
}
